//
//  LRBoxProduceLogic.m
//  lineRunner_cocos2d
//
//  Created by chen jingsen on 13-1-19.
//
//

#import "LRBoxProduceLogic.h"

@implementation LRBoxProduceLogic
@synthesize maxJumpDistance;
@synthesize maxScrollDistance;
@synthesize distanceUnit;
@synthesize differBoxMaxDistance;
@synthesize differBoxMinDistance;
@synthesize sameBoxMaxDistance;
@synthesize sameBoxMinDistance;
@synthesize UpOrDowmBoxRatio;
@synthesize noJumpDistance;
- (id)init
{
    self  = [super init];
    if (self) {
        [self LR_initObjectData];
    }
    return self;
}
//初始化
- (void)LR_initObjectData
{
    maxJumpDistance = 10;
    maxScrollDistance = 10;
    distanceUnit = 3;
    differBoxMaxDistance = 15;
    differBoxMinDistance = 4;
    sameBoxMinDistance = 1;
    sameBoxMaxDistance = 20;
    UpOrDowmBoxRatio = 1/7;
    
    
}

- (float)randomWithStartnum:(float)startnum endNum:(float)endnum
{
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
    [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
    //[dateFormatter setDateFormat:@"hh:mm:ss"]
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS"];
    NSLog(@"Date%@", [dateFormatter stringFromDate:[NSDate date]]);
    [dateFormatter release];
    
    NSTimeInterval distance = [[NSDate date] timeIntervalSince1970];
    NSTimeInterval iDat = distance / (86400) ;
    srand(iDat);
    NSLog( @" From now to  diff: %f ", iDat );
    
    
    NSLog(@"startnum%f endnum:%f random:%f", startnum, endnum, CCRANDOM_0_1()*endnum + startnum);
    return CCRANDOM_0_1()*endnum + startnum;;
}

- (float)randomDistance
{
    float boxDistance;
    
    if ((currentLocation == BoxLocationUp && lastLocation == BoxLocationUp) || (currentLocation == BoxLocationDown && lastLocation == BoxLocationDown)) {
        if (maxJumpDistance - currentObstacleDistance < noJumpDistance) {
            boxDistance = [self randomWithStartnum:noJumpDistance endNum:sameBoxMaxDistance];
        }else{
            boxDistance = [self randomWithStartnum:sameBoxMinDistance endNum:sameBoxMaxDistance];
        }
    }else if ((currentLocation == BoxLocationUp && lastLocation == BoxLocationDown) || (currentLocation == BoxLocationDown && lastLocation == BoxLocationUp)){
        boxDistance = [self randomWithStartnum:differBoxMinDistance endNum:differBoxMaxDistance];
    }
    lastLocation = currentLocation;
    if (boxDistance < noJumpDistance) {
        currentObstacleDistance += noJumpDistance+1;
    }else{
        currentObstacleDistance = 0;
    }
    return boxDistance;
}

#pragma mark - publice method
/**
 *	@brief	获取随机上或下方向
 *
 *	@return	１为上，２为下
 */
- (BoxLocation)boxUpOrDownlocation
{
    currentLocation = (int)[self randomWithStartnum:1 endNum:2];
//    lastLocation = currentLocation;
    return currentLocation;
}

/**
 *	@brief	盒子的处于上一个盒子的距离
 *
 *	@return	距离
 */
- (float)boxDistance
{
    float distance = [self randomDistance]*distanceUnit;
    NSLog(@"distance;%f",distance);
    return distance;
}

@end
